package cn.net.shuxing.yifu.common.service;

import cn.net.shuxing.yifu.common.bean.CityAgentCommission;
import cn.net.shuxing.yifu.common.bean.UserBankCard;
import cn.net.shuxing.yifu.common.bean.UserWithdrawalCash;
import cn.net.shuxing.yifu.common.dto.BaseDto;
import cn.net.shuxing.yifu.common.mapper.CityAgentCommissionMapper;
import cn.net.shuxing.yifu.common.mapper.UserBankCardMapper;
import cn.net.shuxing.yifu.common.mapper.UserWithdrawalCashMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;

@Service
public class CityAgentCommissionService extends BaseService<CityAgentCommission, CityAgentCommissionMapper> {


    @Autowired
    private CityAgentCommissionMapper cityAgentCommissionMapper;

    @Autowired
    private UserWithdrawalCashMapper userWithdrawalCashMapper;

    @Autowired
    private UserBankCardMapper userBankCardMapper;

    public Integer updateByUserId(Long userId, Integer province, Integer city, Integer district, BigDecimal commRate){
        return cityAgentCommissionMapper.updateByUserId(userId,province,city,district,commRate);
    }

    public Integer add(CityAgentCommission cityAgentCommission){
        return cityAgentCommissionMapper.insertSelective(cityAgentCommission);
    }

    public BaseDto selectByUserId(Long userId){
        BaseDto baseDto = new BaseDto();
        CityAgentCommission cityAgentCommission = cityAgentCommissionMapper.selectByUserId(userId);
        if(null != cityAgentCommission){
            baseDto.setData(cityAgentCommission);
        }
        return baseDto;
    }

    /**
     * 更新账户余额（累积佣金，和未结算佣金）
     */
    public BaseDto updateCommission(Long userId, BigDecimal withdrawMoney){
        BaseDto baseDto = new BaseDto();
        CityAgentCommission cityAgentCommission = cityAgentCommissionMapper.selectByUserId(userId);
        if(null != cityAgentCommission && withdrawMoney.compareTo(cityAgentCommission.getCumulativeComm())<=0){
            BigDecimal cumulativeComm = cityAgentCommission.getCumulativeComm().subtract(withdrawMoney);

            Integer m = cityAgentCommissionMapper.updateCumulativeComm(userId,cumulativeComm);
            if(m>0){
                UserWithdrawalCash userWithdrawalCash = new UserWithdrawalCash();
                userWithdrawalCash.setUserId(Integer.valueOf(userId.toString()));
                userWithdrawalCash.setCreateTime(LocalDateTime.now());
                userWithdrawalCash.setType(2);
                userWithdrawalCash.setApplyMoney(Integer.valueOf(withdrawMoney.multiply(new BigDecimal(100)).toString()));
                userWithdrawalCash.setFactMoney(Integer.valueOf(withdrawMoney.multiply(new BigDecimal(100)).toString()));
                //获取用户银行卡信息
                UserBankCard userBankCard = userBankCardMapper.selectUserBankCard(2,Integer.valueOf(userId.toString()));
                if(null != userBankCard){
                    userWithdrawalCash.setApplyBank(userBankCard.getOpenBank());
                    userWithdrawalCash.setApplyBankCardNum(userBankCard.getBankCardNum());
                    userWithdrawalCash.setUserBankCardId(userBankCard.getId());
                }
                userWithdrawalCashMapper.insertSelective(userWithdrawalCash);
                baseDto.setData("提现申请成功");
            }else{
                baseDto.setData("提现申请失败");
            }
        }
        return baseDto;
    }

    /**
     * 城市代理商-业务员管理-结算佣金
     * @param userId
     * @param needMoney
     * @return
     */
    public BaseDto settleSalemanComm(Long userId,BigDecimal needMoney){
        BaseDto baseDto = new BaseDto();
        CityAgentCommission cityAgentCommission = cityAgentCommissionMapper.selectByUserId(userId);
        if(null != cityAgentCommission){
            if(cityAgentCommission.getUnsettledComm().compareTo(needMoney)>=0){
                //扣除未结算佣金金额
                BigDecimal unSettledComm = cityAgentCommission.getUnsettledComm().subtract(needMoney);
                cityAgentCommissionMapper.updateUnsettledComm(userId,unSettledComm);
                //添加累积佣金金额
                BigDecimal cumulativeComm = cityAgentCommission.getCumulativeComm().add(needMoney);
                cityAgentCommissionMapper.updateCumulativeComm(userId,cumulativeComm);
                baseDto.setData("结算成功");
                return baseDto;
            }
        }
        baseDto.setData("结算失败");
        return baseDto;
    }
}
